package com.lfy.config.sharding;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

public class CommentShardingTableConfig implements PreciseShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        Long articleId = preciseShardingValue.getValue();
        articleId = articleId % 4;
        // 根据articleId取模
        for (String each : collection) {
            if (each.endsWith(articleId.toString())) {
                return each;
            }
        }
        throw new IllegalArgumentException("Invalid value");
    }
}
